package com.lefu.es.system;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lefu.es.application.IwellnessApplication;
import com.lefu.es.blenew.helper.BleHelper1;
import com.lefu.es.cache.CacheHelper;
import com.lefu.es.constant.AppData;
import com.lefu.es.constant.BluetoolUtil;
import com.lefu.es.constant.UtilConstants;
import com.lefu.es.db.RecordDao;
import com.lefu.es.entity.NutrientBo;
import com.lefu.es.entity.Records;
import com.lefu.es.entity.UserModel;
import com.lefu.es.event.DeletedRecordsEvent;
import com.lefu.es.event.NoRecordsEvent;
import com.lefu.es.service.ExitApplication;
import com.lefu.es.service.TimeService;
import com.lefu.es.service.UserService;
import com.lefu.es.util.AppBarStateChangeListener;
import com.lefu.es.util.DisplayUtil;
import com.lefu.es.util.FitbitHelper;
import com.lefu.es.util.GoogleFitUtils;
import com.lefu.es.util.LogUtil;
import com.lefu.es.util.LogUtils;
import com.lefu.es.util.MoveView;
import com.lefu.es.util.MyUtil;
import com.lefu.es.util.SPUtil;
import com.lefu.es.util.SharedPreferencesUtil;
import com.lefu.es.util.StringUtils;
import com.lefu.es.util.ToastUtils;
import com.lefu.es.util.UtilTooth;
import com.lefu.es.view.BreakTextView;
import com.lefu.es.view.MyTextView5;
import com.lefu.es.view.guideview.HighLightGuideView;
import com.wellness.scale.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.wellness.scale.R.id.cancle_datacbtn;
import static com.wellness.scale.R.id.save_databtn;

public class BodyFatNewActivity extends BaseBleActivity implements AppBarLayout.OnOffsetChangedListener, View.OnTouchListener {
    
    public static int SELCET_USER = 100;
    protected Dialog receiveDataDialog;
    @Bind(R.id.setting_menu)
    RelativeLayout set;
    @Bind(R.id.bluetooth_status)
    TextView bluetoothStatusTx;
    @Bind(R.id.weith_value_tx)
    MyTextView5 weithValueTx;
    @Bind(R.id.weith_status)
    TextView weithStatus;
    @Bind(R.id.user_name)
    TextView userNameTx;
    @Bind(R.id.bmi_value_tx)
    TextView bmTx;
    @Bind(R.id.bmi_status)
    TextView bmiStatus;
    @Bind(R.id.visal_value_tx)
    TextView visalTx;
    @Bind(R.id.visal_status)
    TextView visalStatus;
    @Bind(R.id.user_header)
    SimpleDraweeView userHeadImg;
    @Bind(R.id.unti_tv)
    TextView unit_tv;
    //private Dialog dialog;
    View view;
    /*体重
   * ---------*/
    @Bind(R.id.face_img_weight)
    ImageView face_img_weight;
    @Bind(R.id.face_img_weight_ll)
    LinearLayout face_img_weight_ll;
    @Bind(R.id.weight_critical_point1)
    TextView weight_critical_point1;
    @Bind(R.id.weight_critical_point2)
    TextView weight_critical_point2;
    @Bind(R.id.biaoz)
    AppCompatTextView biaoz;
    @Bind(R.id.weight_index_tx)
    AppCompatTextView weightIndex;
    @Bind(R.id.status_bar2)
    RelativeLayout status_bar2;
    @Bind(R.id.weight_jiantou)
    AppCompatImageView weight_jiantou;
    /*水分率
    *-----------------
     */
    @Bind(R.id.face_img_moisture)
    ImageView face_img_moisture;
    @Bind(R.id.face_img_moisture_ll)
    LinearLayout face_img_moisture_ll;
    @Bind(R.id.moistrue_critical_point1)
    TextView moistrue_critical_point1;
    @Bind(R.id.moistrue_critical_point2)
    TextView moistrue_critical_point2;
    @Bind(R.id.biaoz_moistrue)
    AppCompatTextView biaoz_moistrue;
    @Bind(R.id.water_index_tx)
    AppCompatTextView waterIndex;
    @Bind(R.id.status_bar_moisture)
    RelativeLayout status_bar_moisture;
    @Bind(R.id.water_jiantou)
    AppCompatImageView water_jiantou;
    /*脂肪率
    *-----------------
     */
    @Bind(R.id.face_img_bft)
    ImageView face_img_bft;
    @Bind(R.id.face_img_bft_ll)
    LinearLayout face_img_bft_ll;
    @Bind(R.id.bft_critical_point1)
    TextView bft_critical_point1;
    @Bind(R.id.bft_critical_point2)
    TextView bft_critical_point2;
    @Bind(R.id.bft_critical_point3)
    TextView bft_critical_point3;
    /*@Bind(R.id.bft_critical_point4)
    TextView bft_critical_point4;*/
    @Bind(R.id.bft_biaoz)
    AppCompatTextView bft_biaoz;
    @Bind(R.id.fat_index_tx)
    AppCompatTextView fatIndex;
    @Bind(R.id.status_bar_bft)
    RelativeLayout status_bar_bft;
    @Bind(R.id.fat_jiantou)
    AppCompatImageView fat_jiantou;
    /*骨量
    *-----------------
     */
    @Bind(R.id.face_img_bone)
    ImageView face_img_bone;
    @Bind(R.id.face_img_bone_ll)
    LinearLayout face_img_bone_ll;
    @Bind(R.id.bone_critical_point1)
    TextView bone_critical_point1;
    @Bind(R.id.bone_critical_point2)
    TextView bone_critical_point2;
    @Bind(R.id.bone_biaoz)
    AppCompatTextView bone_biaoz;
    @Bind(R.id.bone_index_tx)
    AppCompatTextView boneIndex;
    @Bind(R.id.status_bar_bone)
    RelativeLayout status_bar_bone;
    @Bind(R.id.bone_jiantou)
    AppCompatImageView bone_jiantou;
    /*BMI
    *-----------------
     */
    @Bind(R.id.face_img_bmi)
    ImageView face_img_bmi;
    @Bind(R.id.face_img_bmi_ll)
    LinearLayout face_img_bmi_ll;
    @Bind(R.id.bmi_critical_point1)
    TextView bmi_critical_point1;
    @Bind(R.id.bmi_critical_point2)
    TextView bmi_critical_point2;
    @Bind(R.id.bmi_critical_point3)
    TextView bmi_critical_point3;
    @Bind(R.id.bmi_biaoz)
    AppCompatTextView bmi_biaoz;
    @Bind(R.id.bmi_index_tx)
    AppCompatTextView bmiIndex;
    @Bind(R.id.status_bar_bmi)
    RelativeLayout status_bar_bmi;
    @Bind(R.id.bmi_jiantou)
    AppCompatImageView bmi_jiantou;
    /*内脏脂肪指数
    *-----------------
     */
    @Bind(R.id.face_img_visceral)
    ImageView face_img_visceral;
    @Bind(R.id.face_img_visceral_ll)
    LinearLayout face_img_visceral_ll;
    @Bind(R.id.visceral_critical_point1)
    TextView visceral_critical_point1;
    @Bind(R.id.visceral_critical_point2)
    TextView visceral_critical_point2;
    @Bind(R.id.visceral_biaoz)
    AppCompatTextView visceral_biaoz;
    @Bind(R.id.visalfat_index_tx)
    AppCompatTextView visalfatIndex;
    @Bind(R.id.status_bar_visalfat)
    RelativeLayout status_bar_visalfat;
    @Bind(R.id.visalfat_jiantou)
    AppCompatImageView visalfat_jiantou;
    /*BMR基础代谢率
    *-----------------
     */
    @Bind(R.id.face_img_bmr)
    ImageView face_img_bmr;
    @Bind(R.id.face_img_bmr_ll)
    LinearLayout face_img_bmr_ll;
    @Bind(R.id.bmr_critical_point1)
    TextView bmr_critical_point1;
    @Bind(R.id.bmr_biaoz)
    AppCompatTextView bmr_biaoz;
    @Bind(R.id.bmr_index_tx)
    AppCompatTextView bmrIndex;
    @Bind(R.id.status_bar_bmr)
    RelativeLayout status_bar_bmr;
    @Bind(R.id.bmr_jiantou)
    AppCompatImageView bmr_jiantou;
    /*肌肉率
    *-----------------
     */
    @Bind(R.id.face_img_muscle)
    ImageView face_img_muscle;
    @Bind(R.id.face_img_muscle_ll)
    LinearLayout face_img_muscle_ll;
    @Bind(R.id.muscle_critical_point1)
    TextView muscle_critical_point1;
    @Bind(R.id.muscle_critical_point2)
    TextView muscle_critical_point2;
    @Bind(R.id.muscle_biaoz)
    AppCompatTextView muscle_biaoz;
    @Bind(R.id.muscal_index_tx)
    AppCompatTextView muscalIndex;
    @Bind(R.id.status_bar_muscial)
    RelativeLayout status_bar_muscial;
    @Bind(R.id.muscal_jiantou)
    AppCompatImageView muscal_jiantou;
    @Bind(R.id.muscal_name)
    AppCompatTextView muscalName;
    /*身体年龄
    *-----------------
     */
    @Bind(R.id.face_img_age_ll)
    LinearLayout face_img_age_ll;
    @Bind(R.id.physicalage_ly)
    View physicalage_ly;
    @Bind(R.id.age_biaoz)
    AppCompatTextView age_biaoz;
    @Bind(R.id.age_index_tx)
    AppCompatTextView ageIndex;
    @Bind(R.id.age_critical_point)
    TextView age_critical_point;
    @Bind(R.id.age_jiantou)
    AppCompatImageView age_jiantou;
    @Bind(R.id.status_bar_age)
    RelativeLayout status_bar_age;
    @Bind(R.id.history_menu)
    RelativeLayout historyLy;
    @Bind(R.id.history_cicle)
    TextView history_cicle;
    @Bind(R.id.harmbaby_cicle)
    TextView harmbaby_cicle;
    @Bind(R.id.targe_tx)
    TextView targetTx;
    @Bind(R.id.blue_img)
    ImageView blue_img;
    @Bind(R.id.info_item)
    NestedScrollView scrollView;
    List<UserModel> babys = null;
    private AppBarLayout main_appbar;
    private RelativeLayout bmi_circle;
    private RelativeLayout visal_circle;
    private TextView weith_status;
    private RelativeLayout iv_blue_circle;
    private MyTextView5 weith_value_tx1;
    private TextView unti_tv1;
    private RelativeLayout rl_weight1;
    private boolean isActivityShow = false;
    private UserService uservice;
    private TextView tv_comp;
    private TextView tv_comp_water;
    private TextView tv_comp_body_fat;
    private TextView tv_comp_bone;
    private TextView tv_comp_bmi;
    private TextView tv_comp_visalfat;
    private TextView tv_comp_bmr;
    private TextView tv_comp_muscal;
    private TextView tv_comp_age;
    private BreakTextView tv_comp_weight_desc;
    private BreakTextView tv_comp_weight_desc1;
    private BreakTextView tv_comp_water_desc;
    private BreakTextView tv_comp_visalfat_desc;
    private BreakTextView tv_comp_bone_desc;
    private BreakTextView tv_comp_bmr_desc;
    private BreakTextView tv_comp_muscle_desc;
    private BreakTextView tv_comp_fat_desc;
    private BreakTextView tv_comp_fat_desc1;
    private RelativeLayout rl_weight;
    private BreakTextView tv_comp_bmr_desc1;
    private BreakTextView tv_comp_muscle_desc1;
    private BreakTextView tv_comp_water_desc1;
    private BreakTextView tv_comp_bone_desc1;
    private BreakTextView tv_comp_visalfat_desc1;
    private BreakTextView tv_comp_bmi_desc1;
    private BreakTextView tv_comp_age_desc;
    private NestedScrollView info_item;
    private boolean scrollFalg = false;
    private float mLastY;
    private Button cancleBtn, saveBtn;
    private boolean isElectronic = false;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Records data = (Records) msg.obj;
                   LogUtil.e("计算时间差：");
                    long date1 = IwellnessApplication.dilogDate;
                    LogUtil.e("上次的时间是：" + date1);
                    long dateCom = System.currentTimeMillis() - date1;
                    LogUtil.e("时间差是：" + dateCom / 1000 + "秒");
                    if (0!=dateCom && (dateCom / 1000) < 6) {
                        return;
                    }
                    if (null != data && null != data.getReceiveMsg() && RecordDao.isLockData(data.getReceiveMsg())
                            && data.getReceiveMsg().length() == "cf0000e01500000000003b".length()) {
                        //playSound();
                        localData(data, UtilConstants.CURRENT_USER);
                        showReceiveDataDialog();
                    }
                    break;
                case 3:
                    data = (Records) msg.obj;
                    if (null != data) {
                        localData(data, UtilConstants.CURRENT_USER);
                        showReceiveDataDialog();
                    }
                    break;
                case 1:

                    break;
                case 5:
                    /* 退出 */
                    exit();
                    ExitApplication.getInstance().exit(BodyFatNewActivity.this);
                    break;
                case UtilConstants.scaleChangeMessage:
                    /*保存秤类型*/
                    if (UtilConstants.su == null) {
                        UtilConstants.su = new SharedPreferencesUtil(LoadingActivity.mainActivty);
                    }
                    UtilConstants.su.editSharedPreferences("lefuconfig", "scale", UtilConstants.CURRENT_SCALE);
                    /*保存用户信息*/
                    try {
                        uservice.update(UtilConstants.CURRENT_USER);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    /*跳转*/
                    ExitApplication.getInstance().exit(BodyFatNewActivity.this);
                    Intent intent = new Intent();
                    intent.setClass(BodyFatNewActivity.this, LoadingActivity.class);
                    BodyFatNewActivity.this.startActivity(intent);
                    break;
                case 0x99:
                    //physicalage_ly.setVisibility(View.GONE);
                    break;
                case 0x100:
                    //physicalage_ly.setVisibility(View.VISIBLE);
                    break;
                default:
            }
        }
    };

    private View.OnClickListener imgOnClickListener = new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            switch (v.getId()) {
                case cancle_datacbtn:
                    IwellnessApplication.dilogDate = System.currentTimeMillis();
                    if (null != receiveDataDialog) receiveDataDialog.dismiss();
                    receiveDataDialog = null;
                    try {
                        Records lastRecords = recordService.findLastRecords(UtilConstants.CURRENT_USER.getId(), "cf");
                        localData(lastRecords, UtilConstants.CURRENT_USER);
                        localData1(lastRecords, UtilConstants.CURRENT_USER);
                    } catch (Exception e) {
                        LogUtil.e("***e***" + e.getLocalizedMessage());
                    }
                    break;
                case save_databtn:
                    IwellnessApplication.dilogDate = System.currentTimeMillis();
                    try {
                        AppData.hasCheckData = true;
                        if (!BluetoolUtil.bleflag)
                            TimeService.setIsdoing(true);
                        if (null != receiveRecod && null != receiveRecod.getScaleType()) {
                            float compare = 0;
                            if (!TextUtils.isEmpty(receiveRecod.getCompareRecord()) &&
                                    !"null".equals(receiveRecod.getCompareRecord())) {
                                compare = Float.parseFloat(receiveRecod.getCompareRecord());
                            }
                            if (Math.abs(compare) >= 3) {
                                dialogForBodyScale(getString(R.string.receive_data_waring), v.getId());
                                if (null != receiveDataDialog) receiveDataDialog.dismiss();
                                receiveDataDialog = null;
                            } else {
                                if (UtilConstants.KITCHEN_SCALE.equals(UtilConstants.CURRENT_SCALE)) {
                                    NutrientBo nutrient = null;
                                    if (!TextUtils.isEmpty(receiveRecod.getRphoto())) {
                                        nutrient = CacheHelper.queryNutrientsByName(receiveRecod.getRphoto());
                                    }
                                    RecordDao.dueKitchenDate2(recordService, receiveRecod, nutrient);
                                } else {
                                    RecordDao.handleData2(recordService, receiveRecod);
                                    LogUtil.e("*****保存数据***");
                                    FitbitHelper.getInstance().postWeightAndFatData(receiveRecod);
                                }
                                saveDataCallBack(receiveRecod);
                                if (!BluetoolUtil.bleflag) {
                                    TimeService.setIsdoing(false);
                                }
                                if (null != receiveDataDialog) receiveDataDialog.dismiss();
                                // 提示信息
                                if (receiveRecod.getRweight() != 0 && receiveRecod.getRbodyfat() == 0) {
                                    // 第一次接受数据才提示
                                    if (TextUtils.isEmpty(UtilConstants.FIRST_RECEIVE_BODYFAT_SCALE_KEEP_STAND_WITH_BARE_FEET)) {
                                        showAlertDialog(getResources().getString(R.string.keep_stand_with_bare_feet));
                                        if (null == UtilConstants.su) {
                                            UtilConstants.su = new SharedPreferencesUtil(BodyFatNewActivity.this);
                                        }
                                        UtilConstants.su.editSharedPreferences("lefuconfig", "first_badyfat_scale_keep_stand_with_bare_feet", "1");
                                        UtilConstants.FIRST_RECEIVE_BODYFAT_SCALE_KEEP_STAND_WITH_BARE_FEET = "1";
                                    }
                                }
                                receiveDataDialog = null;
                                receiveRecod = null;
                            }
                        }

                    } catch (Exception e) {
                        LogUtil.e( "保存用户测量数据异常" + e.getMessage());
                    }
                    break;
                default:
            }
        }
    };

    private TextView textView1;
    private TextView textView2;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body_fat_new);
        String date = (String) SPUtil.get(BodyFatNewActivity.this, "dialogdate", "0");
        long date1 = Long.valueOf(date);
        IwellnessApplication.dilogDate = date1;
        LogUtil.e("===IwellnessApplication.dilogDate:" + IwellnessApplication.dilogDate);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        uservice = new UserService(this);
        unti_tv1 = (TextView) findViewById(R.id.unti_tv1);
        rl_weight1 = (RelativeLayout) findViewById(R.id.rl_weight1);
        main_appbar = (AppBarLayout) findViewById(R.id.main_appbar);
        main_appbar.addOnOffsetChangedListener(this);
        main_appbar.setVerticalScrollBarEnabled(true);
        bmi_circle = (RelativeLayout) findViewById(R.id.bmi_circle);
        visal_circle = (RelativeLayout) findViewById(R.id.visal_circle);
        weith_value_tx1 = (MyTextView5) findViewById(R.id.weith_value_tx1);
        rl_weight = (RelativeLayout) findViewById(R.id.rl_weight);
        weith_status = (TextView) findViewById(R.id.weith_status);
        iv_blue_circle = (RelativeLayout) findViewById(R.id.weight_circle);
        info_item = (NestedScrollView) findViewById(R.id.info_item);
        info_item.setOnTouchListener(this);

        if (isZh()) {
            biaoz.setVisibility(View.VISIBLE);
            biaoz_moistrue.setVisibility(View.VISIBLE);
            bft_biaoz.setVisibility(View.VISIBLE);
            bone_biaoz.setVisibility(View.VISIBLE);
            visceral_biaoz.setVisibility(View.VISIBLE);
            bmi_biaoz.setVisibility(View.VISIBLE);
            bmr_biaoz.setVisibility(View.VISIBLE);
            muscle_biaoz.setVisibility(View.VISIBLE);
            age_biaoz.setVisibility(View.VISIBLE);

        }

        initView();
        initToolbarLayout();
        ExitApplication.getInstance().addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void updateConnectionState(int resourceId) {
        switch (resourceId) {
            case R.string.disconnected:
                bluetoothStatusTx.setVisibility(View.GONE);
                blue_img.setVisibility(View.GONE);
                break;
            case R.string.connected:
                bluetoothStatusTx.setVisibility(View.VISIBLE);
                blue_img.setVisibility(View.VISIBLE);
                break;
            default:
        }
    }

    @Override
    public void discoverBleService() {
        ToastUtils.ToastCenter(BodyFatNewActivity.this, getString(R.string.scale_paired_success));
        //发送人体参数
        if (null != mDeviceName && (mDeviceName.toLowerCase().startsWith("health"))) {
            handler.sendEmptyMessage(0x100);
            isElectronic = false;
            try {
                if (null != mBluetoothLeService) {
                    final BluetoothGattCharacteristic characteristic =
                            mBluetoothLeService.getCharacteristicNew(mBluetoothLeService.getSupportedGattServices(), "fff4");
                    mBluetoothLeService.setCharacteristicIndiacate(characteristic, true); //开始监听通道
                    //发送用户组数据
                    String unit = "00";
                    if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_ST)) {
                        unit = "02";
                    } else if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_LB)) {
                        unit = "01";
                    } else {
                        unit = "00";
                    }
                    // 获取用户组
                    String p = UtilConstants.CURRENT_USER.getGroup().replace("P", "0");
                    // 获取 校验位
                    String xor = Integer.toHexString(StringUtils.hexToTen("fd") ^ StringUtils.hexToTen("37") ^ StringUtils.hexToTen(unit) ^ StringUtils.hexToTen(p));
                    LogUtil.e("发送新称数据：" + "fd37" + unit + p + "000000000000" + xor);
                    // 发送数据
                    BleHelper1.getInstance().sendDateToScale1(mBluetoothLeService, "fd37" + unit + p + "000000000000" + xor);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (null != mDeviceName && (mDeviceName.toLowerCase().startsWith("electronic"))) {
            BleHelper1.getInstance().sendDateToScale(mBluetoothLeService, MyUtil.getUserInfo());
            handler.sendEmptyMessage(0x99);
            isElectronic = true;
        }
    }

    private String lastReadMessage="";
    /**
     * 接收从秆发过来的数据
     *
     * @param readMessage
     */
    @Override
    public void receiveBleData(String readMessage, String mac, String deviceName) {
        LogUtil.e("***readMessage-->" + readMessage + "***mac-->" + mac + "***deviceName-->" + deviceName);
        if (!isActivityShow) {
            LogUtil.e("*****当前界面没显示*****");
            return;
        }

        //双向通道
        if (isElectronic && readMessage.length()==34){
            UtilConstants.CURRENT_SCALE = UtilConstants.BODY_SCALE;
            LogUtil.e("***readMessage-->" + readMessage);
            String lastStr = readMessage.substring(32,34);
            LogUtil.e("***lastStr-->" + lastStr);
            if (lastStr.equals("f1")){
                MyUtil.setProcessWeightData0(BodyFatNewActivity.this, readMessage.substring(0, 22),
                        weithValueTx, weith_value_tx1, UtilConstants.CURRENT_USER.getDanwei(), false);
                LogUtil.e("***过程数据-->" + readMessage);
            }else if (lastStr.equals("f0") && (TextUtils.isEmpty(lastReadMessage)||!lastReadMessage.equals(readMessage)) ){
                if(System.currentTimeMillis() - UtilConstants.receiveDataTime > 1000) {
                    UtilConstants.receiveDataTime = System.currentTimeMillis();
                    lastReadMessage = readMessage;
                    receiveRecod = MyUtil.parseMessage(this.recordService, readMessage);
                    if (null != receiveRecod) {
                        receiveRecod.setReceiveMsg(readMessage);
                    }
                    Message msg1 = handler.obtainMessage(3);
                    msg1.obj = receiveRecod;
                    handler.sendMessage(msg1);
                    LogUtil.e("***锁定数据-->" + readMessage);
                }
            }else{
                if(System.currentTimeMillis() - UtilConstants.receiveDataTime > 1000) {
                    UtilConstants.receiveDataTime = System.currentTimeMillis();
                    lastReadMessage = readMessage;
                    receiveRecod = MyUtil.parseMessage(this.recordService, readMessage);
                    if (null != receiveRecod) {
                        receiveRecod.setReceiveMsg(readMessage);
                    }
                    Message msg1 = handler.obtainMessage(3);
                    msg1.obj = receiveRecod;
                    handler.sendMessage(msg1);
                    LogUtil.e("***锁定数据-->" + readMessage);
                }
            }
            return;
        }


        /****兼容旧版本**起*/
        if (readMessage.startsWith("0306")) {
            if ((System.currentTimeMillis() - UtilConstants.receiveDataTime > 1000) && null == receiveDataDialog) {
                UtilConstants.receiveDataTime = System.currentTimeMillis();
                LogUtil.e("**接下来收到的数据要兼容旧版本*readMessage**" + readMessage);
                UtilConstants.CURRENT_SCALE = UtilConstants.BODY_SCALE;
                UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
                receiveRecod = MyUtil.parseZuKangMessage(this.recordService, readMessage, UtilConstants.CURRENT_USER);
                LogUtil.e("**receiveRecord**" + receiveRecod);
                Message msg1 = handler.obtainMessage(3);
                msg1.obj = receiveRecod;
                handler.sendMessage(msg1);
            }
            return;
        }
        /****兼容旧版本**终*/

        if (!TextUtils.isEmpty(mac)) {
            mDeviceAddress = mac;
        }
        if (!TextUtils.isEmpty(deviceName)) {
            mDeviceName = deviceName;
        }
        if (readMessage != null && readMessage.length() == 40) {
            readMessage = readMessage.substring(0, 22);
            LogUtil.e( "=====sub后的：" + readMessage);
        }
        LogUtil.e( "reveiveBleData检测读取到数据:" + readMessage);

        //测脂错误
        if (readMessage.startsWith("cb") || readMessage.startsWith("ca")) {

        } else {
            if (TextUtils.isEmpty(readMessage) || readMessage.length() < 10 || !mActivty)
                return;
            if (readMessage.equals(UtilConstants.ERROR_CODE)) {
                if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_ST) || UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_LB)) {
                    Toast.makeText(BodyFatNewActivity.this, getString(R.string.user_data_error), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(BodyFatNewActivity.this, getString(R.string.user_data_error_lb), Toast.LENGTH_LONG).show();
                }
                return;
            } else if (readMessage.equals(UtilConstants.ERROR_CODE_TEST)) {
                Toast.makeText(BodyFatNewActivity.this, getString(R.string.scale_measurement_error), Toast.LENGTH_LONG).show();
                return;
            }
        }

        //秤和人体参数不匹配
        if (readMessage.startsWith("cb") || readMessage.startsWith("ca")) {

        } else {
            if (!ageError) {
                if (UtilConstants.CURRENT_USER.getAgeYear() < 10 || UtilConstants.CURRENT_USER.getBheigth() < 100) {
                    if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_KG)) {
                        showAgeOrHeightAlertDailog(getString(R.string.age_error_4));
                    } else {
                        showAgeOrHeightAlertDailog(getString(R.string.age_error_6));
                    }
                    return;
                }
            }
        }


        //处理不同类型的秤
        boolean newScale = false;
        try {
            if ((readMessage.startsWith("0306"))) {//阿里秤
                newScale = true;
                UtilConstants.CURRENT_SCALE = UtilConstants.BODY_SCALE;
            } else {
                newScale = false;

                /** 称类型判断 */
                String choice_scale = "";
                if ((readMessage.toLowerCase().startsWith(UtilConstants.BODY_SCALE))) {
                    choice_scale = UtilConstants.BODY_SCALE;
                } else if ((readMessage.toLowerCase().startsWith(UtilConstants.BATHROOM_SCALE))) {
                    choice_scale = UtilConstants.BATHROOM_SCALE;
                } else if ((readMessage.toLowerCase().startsWith(UtilConstants.BABY_SCALE))) {
                    choice_scale = UtilConstants.BABY_SCALE;
                } else if ((readMessage.toLowerCase().startsWith(UtilConstants.KITCHEN_SCALE))) {
                    choice_scale = UtilConstants.KITCHEN_SCALE;
                }
                UtilConstants.CURRENT_SCALE = choice_scale;
            }
            LogUtil.e( "mDeviceName:" + mDeviceName);
            if (null != mDeviceName && mDeviceName.toLowerCase().startsWith(UtilConstants.DLscaleName)
                    || (null != mDeviceName && mDeviceName.toLowerCase().startsWith("health"))
                    || (null != mDeviceName && mDeviceName.equals(AppData.LF_SCALE)) && readMessage.length() == 22) { //新的DL Scale
                //CF 88 13 00 14 00 00 00 00 00 40
                LogUtil.e("****readMessage-->" + readMessage);
                if (RecordDao.isLockData(readMessage)) {
                    if ((System.currentTimeMillis()-UtilConstants.receiveDataTime>1000) && null==receiveDataDialog) {
                        LogUtil.e("*****锁定数据******");
                        UtilConstants.receiveDataTime = System.currentTimeMillis();
                        dueData(readMessage, 3);
                        /*if ((!lastReadMessage.equals(readMessage)||TextUtils.isEmpty(lastReadMessage))) {
                            lastReadMessage = readMessage;
                            UtilConstants.receiveDataTime = System.currentTimeMillis();
                            dueData(readMessage, 3);
                        }*/
                    }
                } else {
                    dueData(readMessage, 2);
                }
            } else {
                /**判断是不是两次连续的数据*/
                if (readMessage.length() > 31 && !mDeviceName.toLowerCase().startsWith(UtilConstants.DLscaleName) && (System.currentTimeMillis() - UtilConstants.receiveDataTime > 1000) && null == receiveDataDialog) {
                    UtilConstants.receiveDataTime = System.currentTimeMillis();
                    if (newScale) {
                            /*脂肪秤*/
                        UtilConstants.CURRENT_SCALE = UtilConstants.BODY_SCALE;
                        UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
                        dueData(readMessage, 1);
                    } else {
                        if (readMessage.equals(UtilConstants.ERROR_CODE)) {
                            if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_ST) || UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_LB)) {
                                Toast.makeText(BodyFatNewActivity.this, getString(R.string.user_data_error), Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(BodyFatNewActivity.this, getString(R.string.user_data_error_lb), Toast.LENGTH_LONG).show();
                            }
                            return;
                        } else if (readMessage.equals(UtilConstants.ERROR_CODE_TEST)) {
                            Toast.makeText(BodyFatNewActivity.this, getString(R.string.scale_measurement_error), Toast.LENGTH_LONG).show();
                            return;
                        }
                            /*显示称类型错误*/
                        if (!readMessage.startsWith(UtilConstants.BODY_SCALE) && readMessage.length() > 31) {
                                /*跳转到制定的秤界面*/
                            if (readMessage.startsWith(UtilConstants.BATHROOM_SCALE)) {
                                    /*脂肪秤*/
                                UtilConstants.CURRENT_SCALE = UtilConstants.BATHROOM_SCALE;
                                UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
                                    /*保存测量数据*/
                                RecordDao.dueDate(recordService, readMessage);
                            } else if (readMessage.startsWith(UtilConstants.BABY_SCALE)) {
                                    /*婴儿秤*/
                                UtilConstants.CURRENT_SCALE = UtilConstants.BABY_SCALE;
                                UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
                                    /*保存测量数据*/
                                RecordDao.dueDate(recordService, readMessage);
                            } else if (readMessage.startsWith(UtilConstants.KITCHEN_SCALE)) {
                                    /* 厨房秤 */
                                UtilConstants.CURRENT_SCALE = UtilConstants.KITCHEN_SCALE;
                                UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
                                    /* 保存测量数据 */
                                RecordDao.dueKitchenDate(recordService, readMessage, null);
                            }
                            handler.sendEmptyMessage(UtilConstants.scaleChangeMessage);

                            return;
                        }
                        if (readMessage.equals(UtilConstants.ERROR_CODE_GETDATE)) {
                            openErrorDialog("2");
                            return;
                        }
                        if ((readMessage.startsWith("c") && readMessage.length() > 31)) {
                            dueData(readMessage, 0);
                        }
                    }
                }
            }

        } catch (Exception e) {
            LogUtil.e("解析数据异常" + e.getMessage());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == SELCET_USER) {
            Bundle loginBundle = data.getExtras();
            if (null != loginBundle) {
                Serializable serializable = loginBundle.getSerializable("user");
                if (null != serializable) {
                    UserModel user = (UserModel) serializable;
                    //保存用户
                    UtilConstants.CURRENT_USER = user;
                    if (user.getId() != UtilConstants.CURRENT_USER.getId()) {
                        RecordDao.handleData2(recordService, receiveRecod);
                        FitbitHelper.getInstance().postWeightAndFatData(receiveRecod);
                        initView();
                    } else {
                        RecordDao.handleData2(recordService, receiveRecod);
                        FitbitHelper.getInstance().postWeightAndFatData(receiveRecod);
                    }
                }
            }
        } else if (requestCode == 1) {
            LogUtil.e( "从设置界面返回");
            mActivty = true;
            //startScan();
            initView();

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //mActivty = false;
        isActivityShow = false;
     /*   if (null != mBluetoothLeService) {
            mScanner.stopScan();
            mBluetoothLeService.disconnect();
        }*/
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.e("****onResume*****");
        mActivty = true;
        isActivityShow = true;
        if (null!=UtilConstants.CURRENT_USER){
            try {
                userNameTx.setText(UtilConstants.CURRENT_USER.getUserName());
                if (!TextUtils.isEmpty(UtilConstants.CURRENT_USER.getPer_photo())) {
                    userHeadImg.setImageURI(Uri.fromFile(new File(UtilConstants.CURRENT_USER.getPer_photo())));
                } else {
                    if ("1".equals(UtilConstants.CURRENT_USER.getSex())) {
                        userHeadImg.setImageResource(R.drawable.nan);
                    } else {
                        userHeadImg.setImageResource(R.drawable.nu);
                    }
                }
                if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_LB)
                        || UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    targetTx.setText(UtilTooth.kgToLB_ForFatScale(UtilConstants.CURRENT_USER.getTargweight()) + "lb");
                } else {
                    targetTx.setText(UtilTooth.keep1Point3(UtilConstants.CURRENT_USER.getTargweight()) + "kg");
                }
                Records lastRecords = recordService.findLastRecords(UtilConstants.CURRENT_USER.getId(), "cf");
                localData(lastRecords, UtilConstants.CURRENT_USER);
                localData1(lastRecords, UtilConstants.CURRENT_USER);
                initBodyBar(UtilConstants.CURRENT_USER, lastRecords);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        boolean bindStatus = AppData.checkBindStatus();
        if (!bindStatus && UtilConstants.isFirsShow == 0) {
            bindAlterDialog();
            UtilConstants.isFirsShow = 1;
        }
        startScanBLE();
    }

    @Override
    protected void onStart() {
        super.onStart();
        isActivityShow = true;
        //initBle();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopScan();
        disConnect();
    }

    @Override
    protected void saveDataCallBack(Records records) {
        if (UtilConstants.CURRENT_USER.getGroup().equals("P1")) {
            GoogleFitUtils googleFitUtils = new GoogleFitUtils();
            if (googleFitUtils.isOpenGoogleFit(BodyFatNewActivity.this)) {
                googleFitUtils.insertData(BodyFatNewActivity.this, records.getRweight());
            }
        }
        if (records.getScaleType().startsWith(UtilConstants.BATHROOM_SCALE)) {
                                    /*人体秤*/
            UtilConstants.CURRENT_SCALE = UtilConstants.BATHROOM_SCALE;
            UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
            handler.sendEmptyMessage(UtilConstants.scaleChangeMessage);
        } else if (records.getScaleType().startsWith(UtilConstants.BABY_SCALE)) {
                                    /*婴儿秤*/
            UtilConstants.CURRENT_SCALE = UtilConstants.BABY_SCALE;
            UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
                                    /*保存测量数据*/
            handler.sendEmptyMessage(UtilConstants.scaleChangeMessage);
        } else if (records.getScaleType().startsWith(UtilConstants.KITCHEN_SCALE)) {
                                    /* 厨房秤 */
            UtilConstants.CURRENT_SCALE = UtilConstants.KITCHEN_SCALE;
            UtilConstants.CURRENT_USER.setScaleType(UtilConstants.CURRENT_SCALE);
            handler.sendEmptyMessage(UtilConstants.scaleChangeMessage);
        } else {
            try {
                localData(records, UtilConstants.CURRENT_USER);
                localData1(records, UtilConstants.CURRENT_USER);
                initBodyBar(UtilConstants.CURRENT_USER, records);
                if (TextUtils.isEmpty(UtilConstants.FIRST_INSTALL_BODYFAT_SCALE)) {
                    List<Records> ls = recordService.getAllDatasByScaleAndIDAsc("cf", UtilConstants.CURRENT_USER.getId(), 167f);
                    if (null != ls && ls.size() == 1) {
                        showTipMask();
                    }

                }
            } catch (Exception e) {
                LogUtil.e("保存数据后返回异常：" + e.getMessage());
            }
        }
    }

    private void bindAlterDialog() {
        // 初始化自定义布局参数
        LayoutInflater layoutInflater = getLayoutInflater();
        // 为了能在下面的OnClickListener中获取布局上组件的数据，必须定义为final类型.
        View customLayout = layoutInflater.inflate(R.layout.activity_receive_alert2,
                (ViewGroup) findViewById(R.id.receiveDataDialog));
        cancleBtn = (Button) customLayout.findViewById(R.id.cancle_datacbtn);
        saveBtn = (Button) customLayout.findViewById(R.id.save_databtn);
        textView1 = (TextView) customLayout.findViewById(R.id.textView1);
        textView2 = (TextView) customLayout.findViewById(R.id.textView2);
        textView1.setText(R.string.bind_tishi_4);
        textView2.setText(R.string.bind_tishi_1);
        cancleBtn.setText(R.string.no_btn);
        saveBtn.setText(R.string.yes_btn);

        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                receiveDataDialog.dismiss();
            }
        });
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                receiveDataDialog.dismiss();
                Intent intent = new Intent(BodyFatNewActivity.this, BindScaleActivity.class);
                startActivity(intent);
            }
        });
        receiveDataDialog = new Dialog(this, R.style.dialog);
        receiveDataDialog.setContentView(customLayout);
        receiveDataDialog.show();
        Window window = receiveDataDialog.getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle); // 添加动画
    }

    /**
     * 数据处理
     *
     * @param readMessage
     * @param i
     */
    private void dueData(String readMessage, int i) {
        if (0 == i) {//旧秤
            receiveRecod = MyUtil.parseMessage(this.recordService, readMessage);
            Message msg1 = handler.obtainMessage(3);
            msg1.obj = receiveRecod;
            handler.sendMessage(msg1);
        } else if (1 == i) {//阿里秤
            receiveRecod = MyUtil.parseZuKangMeaage(this.recordService, readMessage, UtilConstants.CURRENT_USER);
            Message msg1 = handler.obtainMessage(3);
            msg1.obj = receiveRecod;
            handler.sendMessage(msg1);
        } else if (2 == i) {//新称过程数据
            MyUtil.setProcessWeightData1(BodyFatNewActivity.this, readMessage, weithValueTx, weith_value_tx1, UtilConstants.CURRENT_USER.getDanwei(), false);
        } else if (3 == i) {//新秤锁定数据
            LogUtil.e("=======新秤锁定数据=======");
            receiveRecod = MyUtil.parseDLScaleMessage(this.recordService, readMessage, UtilConstants.CURRENT_USER);
            if (null != receiveRecod) {
                receiveRecod.setReceiveMsg(readMessage);
            }
            Message msg1 = handler.obtainMessage(0);
            msg1.obj = receiveRecod;
            handler.sendMessage(msg1);
        }
    }

    private boolean isZh() {
        Locale locale = getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return true;
        else
            return false;
    }

    /**
     * 初始化界面参数
     */
    private void initView() {
        tv_comp = (TextView) findViewById(R.id.tv_comp);
        tv_comp_water = (TextView) findViewById(R.id.tv_comp_water);
        tv_comp_body_fat = (TextView) findViewById(R.id.tv_comp_body_fat);
        tv_comp_bone = (TextView) findViewById(R.id.tv_comp_bone);
        tv_comp_bmi = (TextView) findViewById(R.id.tv_comp_bmi);
        tv_comp_visalfat = (TextView) findViewById(R.id.tv_comp_visalfat);
        tv_comp_bmr = (TextView) findViewById(R.id.tv_comp_bmr);
        tv_comp_muscal = (TextView) findViewById(R.id.tv_comp_muscal);
        tv_comp_age = (TextView) findViewById(R.id.tv_comp_age);

        tv_comp_weight_desc = (BreakTextView) findViewById(R.id.tv_comp_weight_desc);
        tv_comp_weight_desc1 = (BreakTextView) findViewById(R.id.tv_comp_weight_desc1);
        tv_comp_water_desc = (BreakTextView) findViewById(R.id.tv_comp_water_desc);
        tv_comp_water_desc1 = (BreakTextView) findViewById(R.id.tv_comp_water_desc1);
        tv_comp_bmi_desc1 = (BreakTextView) findViewById(R.id.tv_comp_bmi_desc1);
        tv_comp_visalfat_desc = (BreakTextView) findViewById(R.id.tv_comp_visalfat_desc);
        tv_comp_visalfat_desc1 = (BreakTextView) findViewById(R.id.tv_comp_visalfat_desc1);
        tv_comp_bone_desc = (BreakTextView) findViewById(R.id.tv_comp_bone_desc);
        tv_comp_bone_desc1 = (BreakTextView) findViewById(R.id.tv_comp_bone_desc1);
        tv_comp_bmr_desc = (BreakTextView) findViewById(R.id.tv_comp_bmr_desc);
        tv_comp_bmr_desc1 = (BreakTextView) findViewById(R.id.tv_comp_bmr_desc1);
        tv_comp_muscle_desc = (BreakTextView) findViewById(R.id.tv_comp_muscle_desc);
        tv_comp_muscle_desc1 = (BreakTextView) findViewById(R.id.tv_comp_muscle_desc1);
        tv_comp_fat_desc = (BreakTextView) findViewById(R.id.tv_comp_fat_desc);
        tv_comp_fat_desc1 = (BreakTextView) findViewById(R.id.tv_comp_fat_desc1);
        tv_comp_age_desc = (BreakTextView) findViewById(R.id.tv_comp_age_desc);

        tv_comp_weight_desc.setAutoSplit(true);
        tv_comp_weight_desc1.setAutoSplit(true);
        tv_comp_water_desc.setAutoSplit(true);
        tv_comp_water_desc1.setAutoSplit(true);
        tv_comp_bmi_desc1.setAutoSplit(true);
        tv_comp_visalfat_desc.setAutoSplit(true);
        tv_comp_visalfat_desc1.setAutoSplit(true);
        tv_comp_bone_desc.setAutoSplit(true);
        tv_comp_bone_desc1.setAutoSplit(true);
        tv_comp_bmr_desc.setAutoSplit(true);
        tv_comp_bmr_desc1.setAutoSplit(true);
        tv_comp_muscle_desc.setAutoSplit(true);
        tv_comp_muscle_desc1.setAutoSplit(true);
        tv_comp_fat_desc.setAutoSplit(true);
        tv_comp_fat_desc1.setAutoSplit(true);
        tv_comp_age_desc.setAutoSplit(true);

        if (null != UtilConstants.CURRENT_USER) {
            try {
                userNameTx.setText(UtilConstants.CURRENT_USER.getUserName());
                if (!TextUtils.isEmpty(UtilConstants.CURRENT_USER.getPer_photo())) {
                    userHeadImg.setImageURI(Uri.fromFile(new File(UtilConstants.CURRENT_USER.getPer_photo())));
                } else {
                    if ("1".equals(UtilConstants.CURRENT_USER.getSex())) {
                        userHeadImg.setImageResource(R.drawable.nan);
                    } else {
                        userHeadImg.setImageResource(R.drawable.nu);
                    }
                }
                if (UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_LB) || UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_FATLB) || UtilConstants.CURRENT_USER.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    targetTx.setText(UtilTooth.kgToLB_ForFatScale(UtilConstants.CURRENT_USER.getTargweight()) + "lb");
                } else {
                    targetTx.setText(UtilTooth.keep1Point3(UtilConstants.CURRENT_USER.getTargweight()) + "kg");
                }
                Records lastRecords = recordService.findLastRecords(UtilConstants.CURRENT_USER.getId(), "cf");
                localData(lastRecords, UtilConstants.CURRENT_USER);
                localData1(lastRecords, UtilConstants.CURRENT_USER);
                initBodyBar(UtilConstants.CURRENT_USER, lastRecords);
                if (lastRecords != null) {
                    LogUtil.e( "======records:" + lastRecords.toString());

                }
                /*if (TextUtils.isEmpty(UtilConstants.FIRST_INSTALL_BODYFAT_SCALE)) {
                    List<Records> ls = recordService.getAllDatasByScaleAndIDAsc("cf", UtilConstants.CURRENT_USER.getId(), 167f);
                    if (null != ls && ls.size() == 1) {
                        showTipMask();
                    }

                }*/

                if (TextUtils.isEmpty(UtilConstants.FIRST_INSTALL_SETTING_PAGER)) {
                    showSettingTipMask();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void initToolbarLayout() {
        main_appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                LogUtils.d("STATE", state.name());
                if (state == State.EXPANDED) {
                    LogUtil.e( "展开状态");
                    rl_weight1.setVisibility(View.GONE);
                    scrollFalg = false;
                    //展开状态
                } else if (state == State.COLLAPSED) {
                    LogUtil.e( "折叠状态");
                    //折叠状态
                    rl_weight1.setVisibility(View.VISIBLE);
                    scrollFalg = true;
                } else {
                    LogUtil.e( "中间状态");
                    rl_weight1.setVisibility(View.GONE);
                    scrollFalg = false;
                }
            }
        });
    }

    /**
     * 初始化 圆圈参数
     *
     * @param
     */
    private void localData(Records record, UserModel user) {
        if (null == user) return;
        if (user.getDanwei().equals(UtilConstants.UNIT_ST)) {
            if (UtilConstants.CURRENT_SCALE.equals(UtilConstants.BODY_SCALE)) {
                String[] tempS = {"0", "0"};
                if (null != record) tempS = UtilTooth.kgToStLbForScaleFat2(record.getRweight());

                weithValueTx.setTexts(tempS[0], tempS[1]);
                if (null != unit_tv) {
                    unit_tv.setText(this.getText(R.string.stlb_danwei));
                }
            } else {
                if (null != record) {
                    weithValueTx.setTexts(UtilTooth.kgToStLb(record.getRweight()), null);
                } else {
                    weithValueTx.setTexts("0", null);
                }
                if (null != unit_tv) {
                    unit_tv.setText(this.getText(R.string.stlb_danwei));
                }
            }
        } else if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)) {
            if (null != record) {
                weithValueTx.setTexts(UtilTooth.kgToLB_ForFatScale(record.getRweight()), null);
            } else {
                weithValueTx.setTexts("0.0", null);
            }
            if (null != unit_tv) {
                unit_tv.setText(this.getText(R.string.lb_danwei));
            }
        } else {
            if (null != record) {
                weithValueTx.setTexts(UtilTooth.keep1Point(record.getRweight()), null);
            } else {
                weithValueTx.setTexts("0.0", null);
            }
            if (null != unit_tv) {
                unit_tv.setText(this.getText(R.string.kg_danwei));
            }
        }
        String sex = user.getSex();
        if (TextUtils.isEmpty(sex) || "null".equalsIgnoreCase(sex)) sex = "1";
        int gender = Integer.parseInt(sex);
        weithStatus.setText(MoveView.weightString(gender, user.getBheigth(), null == record ? 0f : record.getRweight(), this));
        bmTx.setText(String.valueOf(null == record ? "0" : record.getRbmi()));
        visalTx.setText(null == record ? "0.0%" : UtilTooth.keep1Point(record.getRbodyfat()) + "%");
        bmiStatus.setText(MoveView.bmiString(null == record ? 0f : record.getRbmi(), this));
        visalStatus.setText(MoveView.bftString(gender, user.getAgeYear(), null == record ? 0f : record.getRbodyfat(), this));
    }

    /**
     * 初始化 圆圈参数
     *
     * @param
     */
    private void localData1(Records record, UserModel user) {
        if (null == user) return;
        if (user.getDanwei().equals(UtilConstants.UNIT_ST)) {
            if (UtilConstants.CURRENT_SCALE.equals(UtilConstants.BODY_SCALE)) {
                String[] tempS = {"0", "0"};
                if (null != record) tempS = UtilTooth.kgToStLbForScaleFat2(record.getRweight());

                weith_value_tx1.setTexts(tempS[0], tempS[1]);
                if (null != unti_tv1) {
                    unti_tv1.setText(this.getText(R.string.stlb_danwei));
                }
            } else {
                if (null != record) {
                    weith_value_tx1.setTexts(UtilTooth.kgToStLb(record.getRweight()), null);
                } else {
                    weith_value_tx1.setTexts("0", null);
                }
                if (null != unti_tv1) {
                    unti_tv1.setText(this.getText(R.string.stlb_danwei));
                }
            }
        } else if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)) {
            if (null != record) {
                weith_value_tx1.setTexts(UtilTooth.kgToLB_ForFatScale(record.getRweight()), null);
            } else {
                weith_value_tx1.setTexts("0.0", null);
            }
            if (null != unti_tv1) {
                unti_tv1.setText(this.getText(R.string.lb_danwei));
            }
        } else {
            if (null != record) {
                weith_value_tx1.setTexts(UtilTooth.keep1Point(record.getRweight()), null);
            } else {
                weith_value_tx1.setTexts("0.0", null);
            }
            if (null != unti_tv1) {
                unti_tv1.setText(this.getText(R.string.kg_danwei));
            }
        }
    }

    /**
     * 初始化界面所有的进度条
     *
     * @param record
     */
    public void initBodyBar(UserModel user, Records record) {
        if (null != user) {
            try {
                String sex = user.getSex();
                if (TextUtils.isEmpty(sex) || "null".equalsIgnoreCase(sex)) sex = "1";
                int gender = Integer.parseInt(sex);
                /***体重***/
                float weight = (null == record ? 0f : record.getRweight());
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB) || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    weightIndex.setText(UtilTooth.kgToLB_ForFatScale(weight) + "lb");
                } else {
                    weightIndex.setText(UtilTooth.keep1Point(weight) + "kg");
                }
                MoveView.weight(BodyFatNewActivity.this, face_img_weight_ll, face_img_weight, weight_critical_point1, weight_critical_point2, biaoz, gender, user.getBheigth(), weight, user.getDanwei());
                /***水分率***/
                float water = (null == record ? 0f : record.getRbodywater());
                waterIndex.setText(UtilTooth.keep1Point(water) + "%");
                MoveView.moisture(BodyFatNewActivity.this, face_img_moisture_ll, face_img_moisture, moistrue_critical_point1, moistrue_critical_point2, biaoz_moistrue, gender, water);
                /***脂肪率***/
                float bodyfat = (null == record ? 0f : record.getRbodyfat());
                fatIndex.setText(UtilTooth.keep1Point(bodyfat) + "%");
                MoveView.bft(BodyFatNewActivity.this, face_img_bft_ll, face_img_bft, bft_critical_point1, bft_critical_point2, bft_critical_point3, bft_biaoz, gender, user.getAgeYear(), bodyfat);
                /***骨量***/
                float bone = (null == record ? 0f : record.getRbone());
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB) || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    boneIndex.setText(UtilTooth.kgToLB_ForFatScale(bone) + "lb");
                } else {
                    boneIndex.setText(UtilTooth.keep1Point(bone) + "kg");
                }
                MoveView.bone(BodyFatNewActivity.this, face_img_bone_ll, face_img_bone, bone_critical_point1, bone_critical_point2, bone_biaoz, gender, weight, bone, user.getDanwei());
                /***BMI***/
                float bmi = (null == record ? 0f : record.getRbmi());
                bmiIndex.setText(UtilTooth.keep1Point(bmi));
                MoveView.bmi(BodyFatNewActivity.this, face_img_bmi_ll, face_img_bmi, bmi_critical_point1, bmi_critical_point2, bmi_critical_point3, bmi_biaoz, bmi);
                /***内脏脂肪指数***/
                float rvisceralfat = (null == record ? 0f : record.getRvisceralfat());
                visalfatIndex.setText(UtilTooth.keep1Point(rvisceralfat));
                MoveView.visceralFat(BodyFatNewActivity.this, face_img_visceral_ll, face_img_visceral, visceral_critical_point1, visceral_critical_point2, visceral_biaoz, rvisceralfat);
                /***BMR 基础代谢率***/
                float bmr = (null == record ? 0f : record.getRbmr());
                bmrIndex.setText(UtilTooth.keep1Point(bmr) + "kcal");
                MoveView.bmr(BodyFatNewActivity.this, face_img_bmr_ll, face_img_bmr, bmr_critical_point1, bmr_biaoz, gender, user.getAgeYear(), weight, bmr);
                /***肌肉含量***/
                float muscal = (null == record ? 0f : UtilTooth.keep1Point3(record.getRmuscle()));
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB) || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    muscalIndex.setText(UtilTooth.kgToLB_ForFatScale(muscal) + "lb");
                } else {
                    muscalIndex.setText(UtilTooth.keep1Point(muscal) + "kg");
                }
                MoveView.muscle(BodyFatNewActivity.this, face_img_muscle_ll, face_img_muscle, muscle_critical_point1, muscle_critical_point2, muscle_biaoz, gender, user.getBheigth(), muscal, user.getDanwei());
                /***身体年龄***/
                if (null != record && record.getBodyAge() > 0 && record.getBodyAge()<240) {
                    LogUtil.e( "计算身体年龄：" + record.getBodyAge());
                    MoveView.physicAge(BodyFatNewActivity.this, face_img_age_ll, age_critical_point, ageIndex, age_biaoz, user.getAgeYear(), (int) record.getBodyAge());
                    physicalage_ly.setVisibility(View.VISIBLE);
                } else {
                    physicalage_ly.setVisibility(View.GONE);
                }
                compData(record);


                /***体重建议***/
                int weightStaus = MoveView.weightInt(gender, user.getBheigth(), null == record ? 0f : record.getRweight());

                if (weightStaus == 1) {
                    tv_comp_weight_desc.setText(R.string.com_weight_piandi_desc);
                } else if (weightStaus == 2) {
                    tv_comp_weight_desc.setText(R.string.com_weight_biaozhun_desc);
                } else {
                    tv_comp_weight_desc.setText(R.string.com_weight_piangao_desc);
                }

                float weight1 = record.getRweight();
                String weight1Str = "";
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    weight1Str = UtilTooth.kgToLB_ForFatScale(weight1) + "lb" + ",";
                } else {
                    weight1Str = UtilTooth.keep1Point(weight1) + "kg" + ",";
                }
                //当前体重与上次的对比
                String comWeightStr = "";
                float comWeight = Float.parseFloat(record.getCompareRecord());
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    comWeightStr = UtilTooth.kgToLB(comWeight) + "lb";

                } else {
                    comWeightStr = comWeight + "kg";
                }
                if (comWeight >= 0) {
                    comWeightStr = getString(R.string.shangshen) + comWeightStr + "!";
                } else {
                    if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                            || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                        comWeightStr = getString(R.string.xiajiang) + UtilTooth.kgToLB_ForFatScale(Math.abs(comWeight)) + "lb!";
                    } else {
                        comWeightStr = getString(R.string.xiajiang) + UtilTooth.keep1Point(Math.abs(comWeight)) + "kg!";
                    }

                }
                tv_comp_weight_desc1.setText(getString(R.string.tishi_weight) + "" + weight1Str + "" + getString(R.string.com_jirou_desc_5) + comWeightStr);
                //水分率建议
                int waterStatus = MoveView.moistureInt(gender, record.getRbodywater());
                LogUtil.e( "=========waterStatus:" + waterStatus);
                if (waterStatus == 1) {
                    tv_comp_water_desc.setText(R.string.com_water_buzu_desc);
                } else if (weightStaus == 2) {
                    tv_comp_water_desc.setText(R.string.com_water_biaozhun_desc);
                } else {
                    tv_comp_water_desc.setText(R.string.com_water_you_desc);
                }

                //当前水分率与上次的对比
                String comWaterStr = "";
                float comWater = record.getComBodyWater();
                if (comWater >= 0) {
                    comWaterStr = getString(R.string.shangshen) + comWater + "%!";
                } else {
                    comWaterStr = getString(R.string.xiajiang) + Math.abs(comWater) + "%!";
                }
                tv_comp_water_desc1.setText(getString(R.string.tishi_water) + record.getRbodywater() + "%" + "," + getString(R.string.com_jirou_desc_5) + "," + comWaterStr);


                String comBmiStr = "";
                float comBmi = record.getComBmi();
                if (comBmi >= 0) {
                    comBmiStr = getString(R.string.shangshen) + comBmi + "!";
                } else {
                    comBmiStr = getString(R.string.xiajiang) + Math.abs(comBmi) + "!";
                }
                tv_comp_bmi_desc1.setText(getString(R.string.tishi_bmi) + record.getRbmi() + "," + getString(R.string.com_jirou_desc_5) + "," + comBmiStr);
                //内脏脂肪建议
                int visalfatStatus = MoveView.visceralFatInt(record.getRvisceralfat());
                if (visalfatStatus == 1) {
                    tv_comp_visalfat_desc.setText(R.string.com_vifat_biaozhun_desc);
                } else if (visalfatStatus == 2) {
                    tv_comp_visalfat_desc.setText(R.string.com_vifat_jinti_desc);
                } else {
                    tv_comp_visalfat_desc.setText(R.string.com_vifat_weixian_desc);
                }

                //当前内脏脂肪与上次的对比
                String comVisStr = "";
                float comVis = record.getComVisFat();
                if (comVis >= 0) {
                    comVisStr = getString(R.string.shangshen) + comVis + "!";
                } else {
                    comVisStr = getString(R.string.xiajiang) + Math.abs(comVis) + "!";
                }
                tv_comp_visalfat_desc1.setText(getString(R.string.tishi_vfat) + record.getRvisceralfat() + "" + "," + getString(R.string.com_jirou_desc_5) + "," + comVisStr);
                //骨量建议
                int boneStatus = MoveView.boneInt(gender, record.getRweight(), null == record ? 0f : record.getRbone());
                if (boneStatus == 1) {
                    tv_comp_bone_desc.setText(R.string.com_bone_buzu_desc);
                } else if (boneStatus == 2) {
                    tv_comp_bone_desc.setText(R.string.com_bone_biaozhun_desc);
                } else {
                    tv_comp_bone_desc.setText(R.string.com_bone_you_desc);
                }
                //====
                float bone1 = record.getRbone();
                String bone1Str = "";
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    bone1Str = UtilTooth.kgToLB_ForFatScale(bone1) + "lb" + ",";
                } else {
                    bone1Str = UtilTooth.keep1Point(bone1) + "kg" + ",";
                }
                //当前体重与上次的对比
                String comBoneStr = "";
                float comBone = record.getComBone();
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    comBoneStr = UtilTooth.kgToLB_ForBodyScale(comBone) + "lb";
                } else {
                    comBoneStr = UtilTooth.keep1Point(comBone) + "kg";
                }
                if (comBone >= 0) {
                    comBoneStr = getString(R.string.shangshen) + comBoneStr + "!";
                } else {
                    if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                            || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                        comBoneStr = getString(R.string.xiajiang) + UtilTooth.kgToLB_ForFatScale(Math.abs(comBone)) + "lb!";
                    } else {
                        comBoneStr = getString(R.string.xiajiang) + UtilTooth.keep1Point(Math.abs(comBone)) + "kg!";

                    }

                }
                tv_comp_bone_desc1.setText(getString(R.string.tishi_bone) + bone1Str + getString(R.string.com_jirou_desc_5) + "," + comBoneStr);
                int bmrStatus = MoveView.bmrInt(gender, user.getAgeYear(), record.getRweight(), null == record ? 0f : record.getRbmr());
                float bmrBiaoZhunFloat = MoveView.bmrBiaoZhunFloat(gender, user.getAgeYear(), record.getRweight(), record.getRbmr());
                if ("zh".equals(MyUtil.getLanguage(BodyFatNewActivity.this)) || "en".equals(MyUtil.getLanguage(BodyFatNewActivity.this))) {
                    if (bmrStatus == 1) {
                        tv_comp_bmr_desc.setText(getString(R.string.com_bmr_desc_1) + "\n" +
                                getString(R.string.com_bmr_desc_2) + bmrBiaoZhunFloat + "kcal，" + getString(R.string.com_bmr_desc_3) + "\n");
                    } else if (bmrStatus == 3) {
                        tv_comp_bmr_desc.setText(getString(R.string.com_bmr_desc_1) + "\n" +
                                getString(R.string.com_bmr_desc_2) + bmrBiaoZhunFloat + "kcal，" + getString(R.string.com_bmr_desc_4) + "\n");
                    } else {
                        tv_comp_bmr_desc.setText("");
                    }
                }
                //当前BMR与上次的对比
                String comBmrStr = "";
                float comBmr = record.getComBmr();
                if (comBmr >= 0) {
                    comBmrStr = getString(R.string.shangshen) + comBmr + "!";
                } else {
                    comBmrStr = getString(R.string.xiajiang) + Math.abs(comBmr) + "!";
                }
                tv_comp_bmr_desc1.setText(getString(R.string.tishi_bmr) + record.getRbmr() + "" + "," + getString(R.string.com_jirou_desc_5) + " ," + comBmrStr);

                float age = (int) record.getComAge();
                String comAgeStr = "";
                //年龄建议
                if (age >= 0) {
                    int age1 = (int) age;
                    comAgeStr = getString(R.string.shangshen) + age1 + "" + "!";
                } else {
                    int age1 = (int) age;
                    comAgeStr = getString(R.string.xiajiang) + Math.abs(age1) + "" + "!";
                }
                tv_comp_age_desc.setText(getString(R.string.tishi_age) + (int) record.getBodyAge() + "" + "," + getString(R.string.com_jirou_desc_5) + "," + comAgeStr);

                //当前用户的肌肉量
                float rmuscle = record.getRmuscle();
                String rmuscleStr = "";
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    rmuscleStr = UtilTooth.kgToLB(rmuscle) + "lb";
                } else {

                    rmuscleStr = rmuscle + "kg";
                }
                //当前肌肉的对比
                String comRmuscleStr = "";
                float comRmuscle = record.getComMuscle();
                if (user.getDanwei().equals(UtilConstants.UNIT_LB) || user.getDanwei().equals(UtilConstants.UNIT_FATLB)
                        || user.getDanwei().equals(UtilConstants.UNIT_ST)) {
                    comRmuscleStr = UtilTooth.kgToLB(Math.abs(comRmuscle)) + "lb";
                } else {

                    comRmuscleStr = Math.abs(comRmuscle) + "kg";
                }

                if (comRmuscle >= 0) {
                    comRmuscleStr = getString(R.string.shangshen) + comRmuscleStr;
                } else {
                    comRmuscleStr = getString(R.string.xiajiang) + comRmuscleStr;
                }
                int muscleStatus = MoveView.muscleInt(gender, user.getBheigth(), null == record ? 0f : record.getRmuscle());
                if (muscleStatus == 1) {
                    tv_comp_muscle_desc.setText(R.string.com_jirou_desc_1);
                } else if (muscleStatus == 2) {
                    tv_comp_muscle_desc.setText(R.string.com_jirou_desc_2);
                } else {
                    tv_comp_muscle_desc.setText(R.string.com_jirou_desc_3);
                }
                tv_comp_muscle_desc1.setText(getString(R.string.com_jirou_desc_4) + rmuscleStr + "，" + getString(R.string.com_jirou_desc_5) + "," + comRmuscleStr);


                String compFatStr = "0";
                if (record.getComBodyFat() >= 0) {
                    compFatStr = getResources().getString(R.string.shangshen) + record.getComBodyFat() + "%";
                } else {
                    compFatStr = getResources().getString(R.string.xiajiang) + Math.abs(record.getComBodyFat()) + "%";
                }

                int bftInt = MoveView.bftInt(gender, user.getAgeYear(), null == record ? 0f : record.getRbodyfat());
                if (bftInt == 1) {
                    tv_comp_fat_desc.setText(R.string.com_fat_desc1);
                } else if (bftInt == 2) {
                    tv_comp_fat_desc.setText(R.string.com_fat_desc2);
                } else if (bftInt == 3) {
                    tv_comp_fat_desc.setText(R.string.com_fat_desc3);
                } else if (bftInt == 4) {
                    tv_comp_fat_desc.setText(R.string.com_fat_desc4);
                } else {
                    tv_comp_fat_desc.setText(R.string.com_fat_desc4);
                }
                tv_comp_fat_desc1.setText(getString(R.string.com_fat_desc5) + record.getRbodyfat() + "%," + getString(R.string.com_fat_desc6) + "," + compFatStr + "！");

            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }

    private void showSettingTipMask() {
        HighLightGuideView.builder(this).setText(getString(R.string.click_to_setting))
                .addNoHighLightGuidView(R.drawable.ic_ok).addHighLightGuidView(set, 0, 0.5f, HighLightGuideView.VIEWSTYLE_CIRCLE)
                .setTouchOutsideDismiss(false).setOnDismissListener(new HighLightGuideView.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (null == UtilConstants.su) {
                    UtilConstants.su = new SharedPreferencesUtil(BodyFatNewActivity.this);
                }
                UtilConstants.su.editSharedPreferences("lefuconfig", "first_install_setting_pager", "1");
                UtilConstants.FIRST_INSTALL_SETTING_PAGER = "1";
                List<Records> ls = null;
                try {
                    ls = recordService.getAllDatasByScaleAndIDAsc("cf", UtilConstants.CURRENT_USER.getId(), 167f);
                    if (null != ls && ls.size() == 1) {
                        showTipMask();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }).show();
    }

    /**
     * 数据对比显示
     *
     * @param record
     */
    private void compData(Records record) {
        if (Double.parseDouble(record.getCompareRecord()) > 0) {
            tv_comp.setText("↑");
        } else if (Double.parseDouble(record.getCompareRecord()) == 0) {
            tv_comp.setText("-- ");
        } else {
            tv_comp.setText("↓");
        }


        if (record.getComBodyWater() > 0) {
            tv_comp_water.setText("↑");
        } else if (record.getComBodyWater() == 0) {
            tv_comp_water.setText("-- ");
        } else {
            tv_comp_water.setText("↓");
        }

        if (record.getComBodyFat() > 0) {
            tv_comp_body_fat.setText("↑");
        } else if (record.getComBodyFat() == 0) {
            tv_comp_body_fat.setText("-- ");
        } else {
            tv_comp_body_fat.setText("↓");
        }

        if (record.getComBone() > 0) {
            tv_comp_bone.setText("↑");
        } else if (record.getComBone() == 0) {
            tv_comp_bone.setText("-- ");
        } else {
            tv_comp_bone.setText("↓");
        }

        if (record.getComBmi() > 0) {
            tv_comp_bmi.setText("↑");
        } else if (record.getComBmi() == 0) {
            tv_comp_bmi.setText("-- ");
        } else {
            tv_comp_bmi.setText("↓");
        }

        if (record.getComVisFat() > 0) {
            tv_comp_visalfat.setText("↑");
        } else if (record.getComVisFat() == 0) {
            tv_comp_visalfat.setText("-- ");
        } else {
            tv_comp_visalfat.setText("↓");
        }

        if (record.getComBmr() > 0) {
            tv_comp_bmr.setText("↑");
        } else if (record.getComBmr() == 0) {
            tv_comp_bmr.setText("-- ");
        } else {
            tv_comp_bmr.setText("↓");
        }

        if (record.getComMuscle() > 0) {
            tv_comp_muscal.setText("↑");
        } else if (record.getComMuscle() == 0) {
            tv_comp_muscal.setText("-- ");
        } else {
            tv_comp_muscal.setText("↓");
        }

        if (record.getComAge() > 0) {
            tv_comp_age.setText("↑");
        } else if (record.getComAge() == 0) {
            tv_comp_age.setText("-- ");
        } else {
            tv_comp_age.setText("↓");
        }

    }

    private void showTipMask() {
        HighLightGuideView.builder(this).setText(getString(R.string.click_see_data))
                .addNoHighLightGuidView(R.drawable.ic_ok).addHighLightGuidView(history_cicle, 0, 0.5f, HighLightGuideView.VIEWSTYLE_CIRCLE)
                .setTouchOutsideDismiss(false).setOnDismissListener(new HighLightGuideView.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (null == UtilConstants.su) {
                    UtilConstants.su = new SharedPreferencesUtil(BodyFatNewActivity.this);
                }
                UtilConstants.su.editSharedPreferences("lefuconfig", "first_install_bodyfat_scale", "1");
                UtilConstants.FIRST_INSTALL_BODYFAT_SCALE = "1";
                //showHarmBabyTipMask();
            }

        }).show();
    }

    private void showHarmBabyTipMask() {
        HighLightGuideView.builder(this).setText(getString(R.string.click_to_harambaby)).addNoHighLightGuidView(R.drawable.ic_ok)
                .addHighLightGuidView(harmbaby_cicle, 0, 0.5f, HighLightGuideView.VIEWSTYLE_CIRCLE)
                .setTouchOutsideDismiss(false).setOnDismissListener(new HighLightGuideView.OnDismissListener() {
            @Override
            public void onDismiss() {
            }

        }).show();
    }

    @OnClick(R.id.harmbaby_menu)
    public void harmBabyMenuClick() {
        if (null != mScanner) mScanner.stopScan();
        if (null != mBluetoothLeService) {
            mBluetoothLeService.disconnect();
        }
        try {
            if (null == babys || babys.size() == 0) babys = uservice.getAllBabys();
            if (null == babys || babys.size() == 0) {
                //添加一个用户组
                startActivity(BabyAddActivity.creatIntent(BodyFatNewActivity.this, null));
            } else {
                //弹出选择用户组
                startActivity(BabyChoiceActivity.creatIntent(BodyFatNewActivity.this));
            }
        } catch (Exception e) {
            LogUtil.e("脂肪秤页面点击抱婴按钮异常==>" + e.getMessage());
        }
    }

    @OnClick(R.id.setting_menu)
    public void setMenuClick() {
        blue_img.setVisibility(View.GONE);
        bluetoothStatusTx.setVisibility(View.GONE);
        mActivty = false;
        if (null != mScanner) mScanner.stopScan();
        if (null != mBluetoothLeService) {
            mBluetoothLeService.disconnect();
        }
        Intent intent = new Intent(BodyFatNewActivity.this, SettingActivity.class);
        startActivityForResult(intent, 1);
    }

    @OnClick(R.id.history_menu)
    public void historyMenuClick() {
        if (null == UtilConstants.CURRENT_USER) {
            //到用户列表选择页面
            Intent intent1 = new Intent();
            intent1.setClass(BodyFatNewActivity.this, UserListActivity.class);
            BodyFatNewActivity.this.startActivity(intent1);
        } else {
            startActivityForResult(RecordListActivity.creatIntent(BodyFatNewActivity.this, UtilConstants.CURRENT_USER), 0);
        }
    }

    @OnClick(R.id.user_header)
    public void userHeaderClick() {
        if (null != mScanner) mScanner.stopScan();
        if (null != mBluetoothLeService) {
            mBluetoothLeService.disconnect();
        }
        //调到用户列表选择页面
        Intent intent1 = new Intent();
        intent1.setClass(BodyFatNewActivity.this, UserListActivity.class);
        BodyFatNewActivity.this.startActivity(intent1);
    }

    /**
     * 接收到数据提示
     */
    protected void showReceiveDataDialog() {
        LogUtil.e("******showReceiveDataDialog***");
        if (receiveDataDialog==null){
            // 初始化自定义布局参数
            LayoutInflater layoutInflater = getLayoutInflater();
            // 为了能在下面的OnClickListener中获取布局上组件的数据，必须定义为final类型.
            View customLayout = layoutInflater.inflate(R.layout.activity_receive_alert,
                    (ViewGroup) findViewById(R.id.receiveDataDialog));

            cancleBtn = (Button) customLayout.findViewById(R.id.cancle_datacbtn);
            saveBtn = (Button) customLayout.findViewById(save_databtn);

            cancleBtn.setOnClickListener(imgOnClickListener);
            saveBtn.setOnClickListener(imgOnClickListener);

            receiveDataDialog = new Dialog(this, R.style.dialog);
            receiveDataDialog.setContentView(customLayout);
            receiveDataDialog.setCanceledOnTouchOutside(false);
            Window window = receiveDataDialog.getWindow();
            window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
            window.setWindowAnimations(R.style.mystyle); // 添加动画
        }
        if (receiveDataDialog!=null && !receiveDataDialog.isShowing()){
            playSound();
            receiveDataDialog.show();
        }
    }

    protected void dialogForBodyScale(String title, final int id) {
        LogUtil.e( "显示异常数据弹框");
        AlertDialog.Builder builder = new AlertDialog.Builder(BodyFatNewActivity.this);
        builder.setMessage(title);
        builder.setNegativeButton(R.string.cancle_btn, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                try {
                    Records lastRecords = recordService.findLastRecords(UtilConstants.CURRENT_USER.getId(), "cf");
                    localData(lastRecords, UtilConstants.CURRENT_USER);
                    localData1(lastRecords, UtilConstants.CURRENT_USER);
                } catch (Exception e) {
                    LogUtil.e("***e***" + e.getLocalizedMessage());
                }
            }
        });
        builder.setPositiveButton(R.string.ok_btn, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    if (UtilConstants.KITCHEN_SCALE.equals(UtilConstants.CURRENT_SCALE)) {
                        NutrientBo nutrient = null;
                        if (!TextUtils.isEmpty(receiveRecod.getRphoto())) {
                            nutrient = CacheHelper.queryNutrientsByName(receiveRecod.getRphoto());
                        }
                        RecordDao.dueKitchenDate2(recordService, receiveRecod, nutrient);
                    } else {
                        RecordDao.handleData2(recordService, receiveRecod);
                        FitbitHelper.getInstance().postWeightAndFatData(receiveRecod);
                    }
                    saveDataCallBack(receiveRecod);
                    if (!BluetoolUtil.bleflag) {
                        TimeService.setIsdoing(false);
                    }
                    if (null != receiveDataDialog) receiveDataDialog.dismiss();
                    receiveDataDialog = null;
                    receiveRecod = null;
                } catch (Exception e) {
                }
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = builder.create();
        if (alertDialog!=null && alertDialog.isShowing()){
            alertDialog.dismiss();
        }
        alertDialog.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(NoRecordsEvent noRecordsEvent) {
        try {
            //Records lastRecords = recordService.findLastRecords(UtilConstants.CURRENT_USER.getId(),"cf");
            localData(null, UtilConstants.CURRENT_USER);
            localData1(null, UtilConstants.CURRENT_USER);
            initBodyBar(UtilConstants.CURRENT_USER, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DeletedRecordsEvent noRecordsEvent) {
        try {
            if (null != noRecordsEvent && null != noRecordsEvent.getLastRecod()) {
                localData(noRecordsEvent.getLastRecod(), UtilConstants.CURRENT_USER);
                localData1(noRecordsEvent.getLastRecod(), UtilConstants.CURRENT_USER);
                initBodyBar(UtilConstants.CURRENT_USER, noRecordsEvent.getLastRecod());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.layout_weight_jiantou)
    public void menuWeightOpenClick() {
        barBackToDefault();
    }

    private void barBackToDefault() {
        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar2.setVisibility(View.VISIBLE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_muscal_jiantou)
    public void menuMuscalOpenClick() {
        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            LogUtil.e( "到最下面");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            status_bar_muscial.setVisibility(View.VISIBLE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
            handler.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });
        }

    }

    @OnClick(R.id.layout_visalfat_jiantou)
    public void menuVisalfatOpenClick() {
        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_visalfat.setVisibility(View.VISIBLE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
            handler.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_bmi_jiantou)
    public void menuBmiOpenClick() {
        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_bmi.setVisibility(View.VISIBLE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_bone_jiantou)
    public void menuBoneOpenClick() {
        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_bone.setVisibility(View.VISIBLE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_fat_jiantou)
    public void menuFatOpenClick() {
        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_bft.setVisibility(View.VISIBLE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_water_jiantou)
    public void menuWaterOpenClick() {
        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_moisture.setVisibility(View.VISIBLE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_bmr_jiantou)
    public void menuBmrOpenClick() {
        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_bmr.setVisibility(View.VISIBLE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
            handler.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });

        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @OnClick(R.id.layout_age_jiantou)
    public void menuAgeOpenClick() {
        if (status_bar_age.getVisibility() == View.VISIBLE) {
            status_bar_age.setVisibility(View.GONE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        } else {
            status_bar_age.setVisibility(View.VISIBLE);
            age_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.up_arrow));
            handler.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });

        }

        if (status_bar_bmr.getVisibility() == View.VISIBLE) {
            status_bar_bmr.setVisibility(View.GONE);
            bmr_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_muscial.getVisibility() == View.VISIBLE) {
            status_bar_muscial.setVisibility(View.GONE);
            muscal_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_visalfat.getVisibility() == View.VISIBLE) {
            status_bar_visalfat.setVisibility(View.GONE);
            visalfat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bmi.getVisibility() == View.VISIBLE) {
            status_bar_bmi.setVisibility(View.GONE);
            bmi_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bone.getVisibility() == View.VISIBLE) {
            status_bar_bone.setVisibility(View.GONE);
            bone_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_bft.getVisibility() == View.VISIBLE) {
            status_bar_bft.setVisibility(View.GONE);
            fat_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar_moisture.getVisibility() == View.VISIBLE) {
            status_bar_moisture.setVisibility(View.GONE);
            water_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }

        if (status_bar2.getVisibility() == View.VISIBLE) {
            status_bar2.setVisibility(View.GONE);
            weight_jiantou.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.down_arrow));
        }
    }

    @Override
    protected void onRestart() {
        LogUtil.e( "=====onRestart========");
        super.onRestart();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            handler.sendEmptyMessage(5);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME) {
            exit();
            ExitApplication.getInstance().exit(this);
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 退出
     */
    private void exit() {
        /* 停止服务 */
        //stopScanService();
        NotificationManager notificationManager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(0);
        this.finish();
    }

    /**
     * 计算太阳的半径dp转px
     */
    private float getRadius(Context context, float width) {
        float width1 = DisplayUtil.dip2px(context, width);
        return width1 / 2;
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        LogUtil.d("====verticalOffset:" + verticalOffset);
        int verticalOffset1 = DisplayUtil.dip2px(BodyFatNewActivity.this, verticalOffset);
        int jiaodu = Math.abs(verticalOffset1 / DisplayUtil.dip2px(BodyFatNewActivity.this, 2));
        LogUtil.d("====角度:" + jiaodu);
        if (jiaodu == 90) {
            scrollFalg = true;
        } else {
            scrollFalg = false;
        }
        if (jiaodu >= 90) {
            LogUtil.e("=========" + verticalOffset1);
            ViewCompat.setRotationX(bmi_circle, 90);
            ViewCompat.setRotationX(visal_circle, 90);
            ViewCompat.setRotationX(rl_weight, 90);
            ViewCompat.setRotationX(weith_status, 90);
            ViewCompat.setRotationX(iv_blue_circle, 90);
            rl_weight1.setVisibility(View.VISIBLE);

        } else {
            scrollFalg = false;
            ViewCompat.setRotationX(bmi_circle, jiaodu);
            ViewCompat.setRotationX(visal_circle, jiaodu);
            ViewCompat.setRotationX(rl_weight, jiaodu);
            ViewCompat.setRotationX(weith_status, jiaodu);
            ViewCompat.setRotationX(iv_blue_circle, jiaodu);
            //  ViewCompat.setY(tv_show_weight, Math.abs(jiaodu + 60));
            rl_weight1.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int eventaction = event.getAction();
        switch (eventaction) {
            case MotionEvent.ACTION_DOWN:
                LogUtil.e( "MotionEvent.ACTION_DOWN:");
                return false;
            case MotionEvent.ACTION_MOVE:
                LogUtil.e( "MotionEvent.ACTION_MOVE:");
                final float y = event.getY();
                if (y > mLastY) {
                    LogUtil.e( "向下");
                    scrollFalg = false;
                } else {
                    LogUtil.e( "向上");
                }
                mLastY = y;
                if (scrollFalg) {
                    return true;
                } else {
                    return false;
                }
            case MotionEvent.ACTION_UP:
                LogUtil.e( "MotionEvent.ACTION_UP:");
                scrollFalg = false;
                return false;
            default:
        }
        return false;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                this.exitApp();
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    /**
     * 退出程序
     */
    private void exitApp() {
        // 判断2次点击事件时间
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(BodyFatNewActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finishAffinity();
        }
    }

}
